<template>
  <view class="yh-page">
  		<!-- 轮播图 -->
  		<yh-swiper v-if="bannerList.length > 0" :bannerListArray="bannerList"/>
  		<!-- 商品列表 -->
  		<yh-goods-list :goodsList="goodsList" />
  		<!-- 下拉刷新 -->
  		<view class="uni-loadmore" v-if="showLoadMore">{{loadMoreText}}</view>
  </view>
</template>

<script>
  import { checkApiLogin,checkToken,checkPhone,checkOpenid,checkAll } from '@/utils/chekLogin'
  import { isWechat } from '@/utils/wechat'
  import {  getUserInfo, wecahtOfficialAuthToLogin,wecahtOfficialAuth} from '@/api/login/login'
  import { getIndexBannerWithoutLogin,getIndexGoodsWithoutLogin } from '@/api/youhu/user/index/userIndex'
  import { loginByUserId } from '@/api/test/login/login'
  import { mapGetters } from 'vuex';
  
  export default 
  {
    //计算属性
    computed:
    {
		...mapGetters(['token','phone','openid','user_id','real_auth_staus','currentRole','roles'])
    },
    //监听
    watch:
    {
		
    },
	//组件
	components:
	{
	},
    data() 
    {
    	return{
			//轮播图
			bannerList:[],
			bannerWhere:{
				//当前页面
				currentPage:1,
				//页面内容数量
				pageSize:10,
				//排序方式
				sortType:2,
			},
			goodsWhere:{
				//当前页面
				currentPage:1,
				//页面内容数量
				pageSize:10,
				//排序方式
				sortType:2,
			},
			//产品容器
			goodsList:[],
			//下来刷新
			loadMoreText: "加载中...",
			showLoadMore: false,
			loadMoreMax:0,
			
        };
    },
    //页面加载
    async onLoad(options) 
    {
		 checkApiLogin()
		 
		 //第一步:是否登录
		 
		 if(this.token)
		 {
		 	//console.log('登录')
		 	//console.log('token'+this.token) 
			//第二步:获取用户信息
			await this.getUserInfo()
			
			checkAll()
		 	// #ifdef H5
		 	if(options)
		 	{
		 		if(options.hasOwnProperty('code') && options.hasOwnProperty('state'))
		 		{
		 			
		 			if(isWechat())
		 			{
						//如果没有openid再去检测
						if(!this.openid)
						{
							let _this = this
							
							//静默授权
							if(options.state == 10)
							{
								await this.wecahtOfficialAuth(options)
							}
							
							//主动授权
							if(options.state == 20)
							{
								await this.wecahtOfficialAuthToLogin(options)
							}
						}
		 				
		 			}
		 		}
		 	
		 	}
		 	// #endif
		 	
		 	
		 }
		 else
		 {
		 	//console.log('未登录') 
		 	// #ifdef H5
		 	if(options)
		 	{
		 		if(options.hasOwnProperty('code') && options.hasOwnProperty('state'))
		 		{
		 			//主动授权
		 			if(options.state == 20)
		 			{
		 				await this.wecahtOfficialAuthToLogin(options)
		 				
		 				//第二步:获取用户信息
		 				await this.getUserInfo()
		 				
		 				//第三步:是否绑定了手机号
		 				checkPhone()
		 				//第四步:是否微信授权了(H5下)是否绑定了openid
		 				checkOpenid()
		 			}
		 		}
		 		
		 		if(options.hasOwnProperty('invite_code') )
		 		{
		 			uni.setStorageSync('invite_code', options.invite_code);
		 		}
		 		
		 		if(options.hasOwnProperty('inviteCode'))
		 		{
		 			uni.setStorageSync('invite_code', options.inviteCode);
		 		}
		 	}
		 	
		 	// #endif
		 	
		 	//调用未登录的接口
		 }
		 //获取首页轮播图
		 this.getIndexBanner()
		 //获取首页所有商品
		 this.getIndexAllGoods()
    },
	//下来刷新
	onPullDownRefresh()
	{
		console.log('onPullDownRefresh');
		
		//this.getIndexGoods()
		
		
	},
	//页面触底
	onReachBottom()
	{
		//console.log('onReachBottom')
		this.showLoadMore = true;
		uni.startPullDownRefresh();
		
		if(this.loadMoreMax >= this.goodsList.length )
		{
			this.loadMoreText = "没有更多数据了!"
			
			setTimeout(function() 
			{
				uni.stopPullDownRefresh()
				
			}, 1000);
			
			return;
		}
		this.goodsWhere.currentPage += 1
	
		this.getIndexGoods()
	},
    //页面显示
    onShow() 
    {
		
    },
    //页面初次渲染完成
    onReady()
    {

    },
    //页面隐藏
    onHide()
    {

    },
    //页面卸载
    onUnload()
    {
		this.loadMoreMax = 0
		this.loadMoreText = "加载更多",
		this.showLoadMore = false;
    },
    methods:{
		//静默授权
		async wecahtOfficialAuth(options)
		{
			const result = await wecahtOfficialAuth(options)
			
			if(result && result.code === 0)
			{
				const {openid} = result.data
				
				this.$store.dispatch('user/authSaveOpenid',openid)
			}
			else
			{
				notice('授权失败')
			}
		},
		//主动授权
		async wecahtOfficialAuthToLogin(options)
		{
			const result = await wecahtOfficialAuthToLogin(options)
			
			if(result && result.code === 0)
			{
				console.log('主动授权结果')
				console.log(result)
				const {openid,token} = result.data
				
				this.$store.dispatch('user/authSaveOpenid',openid)
				this.$store.dispatch('user/authSaveToken',token)
			}
			else
			{
				notice('授权失败')
			}
		},
		//获取用户信息
		async getUserInfo()
		{
			//获取用户信息
			const result = await getUserInfo()
			
			if(result && result.code === 0)
			{
				this.$store.dispatch('user/saveUserInfo',result.data)
			}
			else
			{
				notice('获取信息失败')
			}
		},
		//获取用户信息
		handleToActionCheckAuth()
		{
			checkAll()
		},
		// 获取首页轮播图
		getIndexBanner()
		{
			const _this = this
			getIndexBannerWithoutLogin(this.bannerWhere).then(res=>
			{
				return new Promise((resolve,reject)=>
				{
					if(res && res.code === 0)
					{
						resolve(res.data)
						
						_this.bannerList = res.data
						
						//console.log(_this.bannerList);
					}
				})
			})
		},
		//获取首页所有产品
		getIndexAllGoods()
		{
			this.getIndexGoods()
		
		},
		//获取首页正常产品
		getIndexGoods()
		{
			const _this = this
			
			getIndexGoodsWithoutLogin(this.goodsWhere).then(res=>{
				
				return new Promise((resolve,reject)=>
				{
					if(res && res.code === 0)
					{
						resolve(res.data)
						
						//console.log(res.data)
						
						let newGoodsList = _this.goodsList
						
						let showGoodsList = []
						
						if(res.data.length > 0)
						{
							showGoodsList = [...newGoodsList,...res.data]
						}
						else
						{
							showGoodsList = newGoodsList
						}
						
						_this.goodsList = showGoodsList
						_this.loadMoreMax = res.meta.total
					}
				})
			})
		}
		
    }
  }
</script>
<style lang="scss" scoped>
	
</style>
